﻿Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Management.Automation

<Cmdlet(VerbsCommon.Add, TeamSystemNouns.WorkItemAttachment, ConfirmImpact:=ConfirmImpact.Low, SupportsShouldProcess:=True)> _
Public Class AddWorkItemAttachment
    Inherits TeamFoundationServerCmdletBase

    Public Sub New()
    End Sub

    Private _id As Integer

    <Parameter(Position:=1, Mandatory:=True, ValueFromPipelineByPropertyName:=True), ValidateRange(1, Integer.MaxValue)> _
    Public Property Id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Private _path As String

    <Parameter(Position:=2, Mandatory:=True, ValueFromPipelineByPropertyName:=True), ValidateNotNullOrEmpty()> _
    Public Property Path() As String
        Get
            Return _path
        End Get
        Set(ByVal value As String)
            _path = value
        End Set
    End Property

    Private _comment As String

    <Parameter(Position:=3, ValueFromPipelineByPropertyName:=True), ValidateNotNullOrEmpty()> _
    Public Property Comment() As String
        Get
            Return _comment
        End Get
        Set(ByVal value As String)
            _comment = value
        End Set
    End Property

    Private _passThru As SwitchParameter

    <Parameter()> _
    Public Property PassThru() As SwitchParameter
        Get
            Return _passThru
        End Get
        Set(ByVal value As SwitchParameter)
            _passThru = value
        End Set
    End Property

    Protected Overrides Sub ProcessRecord()
        Dim workItem As WorkItem
        workItem = workItemStore.GetWorkItem(Id)
        workItem.Attachments.Add(New Attachment(Path, Comment))

        If ShouldProcess(Id.ToString()) Then
            workItem.Save()
        End If

        If PassThru.IsPresent Then
            workItem.SyncToLatest()
            WriteObject(workItem)
        End If
    End Sub
End Class